package Scuttlebutt;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;

class NodeTransferable implements Transferable{
	
	
	ArrayList<Object> list = null;
	DataFlavor[] flavors = null; 
	
	public NodeTransferable(ArrayList list){
		this.list = list;
		try{
			flavors[0] = new DataFlavor("application/node");
		}catch(java.lang.ClassNotFoundException e){
		}
		System.out.println("NodeTransferable");
	}	
	
	public DataFlavor[] getTransferDataFlavors(){
		return flavors;
	}
	
	public boolean isDataFlavorSupported(DataFlavor flavor){
		for(DataFlavor f : flavors){
			if(f.equals(flavor))
				return true;
		}
		return false;
	}
		
	public Object getTransferData(DataFlavor flavor) 
		throws UnsupportedFlavorException, IOException	{
			return list;
	}
	
}


public class TreeTransferHandler extends TransferHandler 
{
	public TreeTransferHandler()
	{
	}
	
	protected Transferable createTransferable(JComponent c){
//		System.out.println("createTransferable");
		ArrayList<Object> list = new ArrayList<Object>();
		JTree tree = (JTree)c;
		TreePath[] paths = tree.getSelectionPaths();
		int count = 1;
		for(TreePath p : paths){
////			System.out.println("count:" + count++);
			Object o = p.getLastPathComponent();
			list.add(o);
//			System.out.println("node dragged:" + o.toString());
		}
		
		return new NodeTransferable(list);
	}
	
    public int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }
	
	public boolean importData(JComponent comp, Transferable t){
		return true;//..super.importData(comp, t);
	}
    //Remove the old text if the action is a MOVE.
    //However, we do not allow dropping on top of the selected text,
    //so in that case do nothing.
    protected void exportDone(JComponent c, Transferable data, int action) {
	    System.out.println("exportDone");
 /*       if (shouldRemove && (action == MOVE)) {
            if ((p0 != null) && (p1 != null) &&
                (p0.getOffset() != p1.getOffset())) {
                try {
                    JTextComponent tc = (JTextComponent)c;
                    tc.getDocument().remove(
                       p0.getOffset(), p1.getOffset() - p0.getOffset());
                } catch (BadLocationException e) {
                    System.out.println("Can't remove text from source.");
                }
            }
        }
        source = null;*/
    }
	
}

